package licode.unisop.platform.application.uniclient;

import cn.hutool.core.bean.BeanUtil;
import licode.unisop.client.api.SopPlatformAuth;
import licode.unisop.client.info.SopIdInfo;
import licode.unisop.client.info.SopPlatAuthInfo;
import licode.unisop.client.info.SopPlatHeartInfo;
import licode.unisop.client.vo.SopAuthPlatform;
import licode.unisop.platform.client.api.SiPlatformAuthService;
import licode.unisop.platform.client.info.PlatformAuthInfo;
import licode.unisop.platform.client.info.PlatformHeartInfo;
import licode.unisop.platform.client.vo.PlatAuthVO;
import licode.unisop.provider.info.SiPlatIdFind;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 实现默认的平台注册接口
 *
 * @author licode
 */

@Primary
@Component
public class SopPlatformAuthImpl implements SopPlatformAuth {
    @Resource
    private SiPlatformAuthService siPlatformAuthService;

    @Override
    public SopAuthPlatform register(SopPlatAuthInfo info) {
        PlatAuthVO platAuth;

        platAuth = siPlatformAuthService.register(PlatformAuthInfo
                .builder()
                .realmId(info.getRealmId())
                .id(info.getId())
                .secret(info.getSecret())
                .build());

        return BeanUtil.copyProperties(platAuth, SopAuthPlatform.class);
    }

    @Override
    public SopAuthPlatform fromId(SopIdInfo idInfo) {
        PlatAuthVO authVO = siPlatformAuthService.fromId(SiPlatIdFind.builder()
                .platformId(idInfo.getId())
                .build());
        return null == authVO ? null : BeanUtil.copyProperties(authVO, SopAuthPlatform.class);
    }

    @Override
    public long getAuthCount(SopIdInfo idInfo) {
        return siPlatformAuthService.getAuthCount();
    }

    @Override
    public void forbid(SopIdInfo idInfo) {

    }

    @Override
    public String renew(SopPlatHeartInfo req) {
        PlatformHeartInfo info;

        info = BeanUtil.copyProperties(req, PlatformHeartInfo.class);
        return siPlatformAuthService.renew(info);
    }
}
